// 引入cors模块，用来解决跨域
const cors = require("cors");
// 引入express模块
var express = require("express");
// 引入path模块，用于路径处理
var path = require("path");
// 用于打印日志
var logger = require("morgan");
// 引入的路由器
var indexRouter = require("./routes/index");
var usersRouter = require("./routes/users");
const feeRouter = require("./routes/fee");
const parkingRouter = require("./routes/parking");
const chatroomRouter = require("./routes/chatroom");
const addrRouter = require("./routes/addr");
const noticeRouter = require("./routes/notice");
const medicalRouter = require("./routes/medical");
// 创建WEB服务器
var app = express();

// 开发模式下打印日志
app.use(logger("dev"));
// 将post传参格式为json，{"a":1,"b":2} 转为对象
app.use(express.json());
// 将post传参格式为 a=1&b=2，转为对象{a:1,b:2}
app.use(express.urlencoded({ extended: true }));

// 使用cors模块来解决跨域
app.use(cors());
// 托管静态资源到public目录
app.use(express.static(path.join(__dirname, "public")));
// 注册路由器模块
app.use("/", indexRouter);
app.use("/users", usersRouter);
app.use("/fee", feeRouter);
// 注册智慧停车路由器模块，添加前缀/parking
app.use("/parking", parkingRouter);
app.use("/chatroom", chatroomRouter);
app.use("/addr", addrRouter);
app.use("/notice", noticeRouter);
app.use("/medical", medicalRouter);

// 错误处理中间件,可以拦截所有路由中传递的错误
app.use(function (err, req, res, next) {
  // err 接收到传递过来的错误信息
  console.log(err);
  res.send({ code: 500, msg: "服务器端错误" });
});

module.exports = app;
